package cn.stronglink.asset.terminal.bjc.repair.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import cn.stronglink.asset.module.assetmanage.repair.mapper.RepairMapper;
import cn.stronglink.asset.terminal.bjc.repair.mapper.RepairApiMapper;
import cn.stronglink.asset.terminal.bjc.repair.vo.RepairApiVo;
import cn.stronglink.asset.terminal.bjc.repair.vo.RepairDetailApiVo;

@Service
@Transactional(rollbackFor=Exception.class) 
public class RepairApiService {
	
	@Autowired
	private RepairMapper repairMapper;
	@Autowired
	private RepairApiMapper repairApiMapper;
	
	public List<RepairApiVo> qryRepairList(Map<String, Object> params) {
		List<RepairApiVo> list = repairApiMapper.queryRepairListByParams(params);
		if(list!=null&&list.size()>0) {
			for(RepairApiVo repair:list) {
				List<RepairDetailApiVo> detailList = repairApiMapper.qryDetailByRepairId(repair.getId());
				repair.setRepairDetailApiList(detailList);
			}
		}
		return list;
	}

	public void doRepair(RepairApiVo repair) {
		if(repair.getId()!=null) {
			repair.setStatus(3);
			repair.setUpdateBy(repair.getOperatorBy());
			repair.setUpdateTime(new Date());
			repairMapper.updateRepair(repair);
		}else {
			repair.setId(IdWorker.getId());
			repair.setRoomId(repair.getRoomId());
			repair.setExecuteTime(new Date());
			Long uid = repair.getCreateBy();
			repair.setOperatorBy(uid);
			repair.setStatus(3);
			repair.setCreateTime(new Date());
			repair.setRemark("手持机创建");
			repairMapper.insertRepair(repair);
		}
		
		if(repair.getRepairDetailApiList()!=null&&repair.getRepairDetailApiList().size()>0) {
			for(RepairDetailApiVo detail:repair.getRepairDetailApiList()) {
				if(detail.getId()!=null) {
					detail.setStatus(1);
					detail.setUpdateBy(repair.getOperatorBy());
					detail.setUpdateTime(new Date());
					repairMapper.updateRepairDetail(detail);
				}else {
					detail.setId(IdWorker.getId());
					detail.setRepairId(repair.getId());
					detail.setAssetId(Long.parseLong(detail.getAssetId_()));
//					detail.setRemark(detail.getRemark());
//					detail.setRepairRemark(detail.getRepairRemark());
					detail.setStatus(1);
					detail.setCreateBy(repair.getCreateBy());
					detail.setCreateTime(new Date());
					repairMapper.insertRepairDetail(detail);
				}
			}
		}
	}

	public void remove(Long id) {
		repairMapper.remove(id);
		repairMapper.removeRepairDetail(id);
	}
	
}
